{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 演化一个类\n",
    "\n",
    "下面以如何表示“日期”这个概念展开讨论如何演化一个类。\n",
    "\n",
    "## 结构和函数\n",
    "\n",
    "可以使用结构表示“日期”："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct Date {\n",
    "    int y; // 年\n",
    "    int m; // 月\n",
    "    int d; // 日\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "Date today; // 命名对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "@0x7f69c9cfe028"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "today"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以使用 `date` 对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "today.y = 2022;\n",
    "today.m = 12;\n",
    "today.d = 27;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`today` 对象可以表示为：\n",
    "\n",
    "```{mermaid}\n",
    "stateDiagram-v2\n",
    "    state today {\n",
    "    y --> 2022\n",
    "    m --> 12\n",
    "    d --> 27\n",
    "    }\n",
    "```\n",
    "\n",
    "### 辅助函数\n",
    "\n",
    "为了操作一个类对象，需要定义一系列辅助函数。\n",
    "\n",
    "比如，检查用户输入的日期是否格式正确，如何获得日期的后面的日期？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "void init_day(Date& dd, int y, int m, int d)\n",
    "{\n",
    "    // 检查 (y,m,d) 是否合法\n",
    "    // 如果是的话，用其来初始化 dd\n",
    "    dd.y = y;\n",
    "    dd.m = m;\n",
    "    dd.d = d;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "void add_day(Date& dd, int n)\n",
    "{\n",
    "    // 为 日期 dd 添加 n 天\n",
    "    dd.d += n;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "Date today;\n",
    "init_day(today, 2021, 12, 1); // 检查错误的行为，当下只能人工检查\n",
    "add_day(today, 5);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 成员函数和构造函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a2954483fe4136a94bd721da8601251ae4c667fedb6adcfe70ff0183b19bacbc"
  },
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
